package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IAuthCode;
import com.wbao.dianniu.listener.IAuthCodeListener;
import com.wbao.dianniu.listener.IForgetPass;
import com.wbao.dianniu.listener.IForgetPassListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.Utils;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;

public class ForgetPassManager implements IForgetPass,IAuthCode{
	private IForgetPassListener mListener = null;
	private IAuthCodeListener authListener = null;
	private Context mContext;
	public ForgetPassManager(Context context) {
		mContext = context;
	}
	
	@Override
	public void getAuthCode(Context context, String mobile) {
		StringEntity stringEntity = null;
		try {
			Command command = new Command();
			command.addArgs("mobile", mobile).addAction(Action.ACTION_FORGETPWD_CODE);
			stringEntity = new StringEntity(Batch.build().addCommand(command).toJson(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		HttpUtil http = new HttpUtil();
		http.req(mContext, stringEntity, new MyResponse() {
			
			@Override
			public void responseSucess(Object obj) {
				// TODO Auto-generated method stub
				if (null != authListener)
					authListener.onAuthCodeSuccess(obj);
			}
			
			@Override
			public void responseFailure(int errorCode, String msg) {
				// TODO Auto-generated method stub
				if (null != authListener)
					authListener.onAuthCodeFailure(errorCode, msg);
			}
		});
	}
	
	
	@Override
	public void forgetPwd(Context context, String mobile, String code,
			String password) {
		// TODO Auto-generated method stub
		StringEntity stringEntity = null;
		try {
			Batch batch = new Batch();
			batch.setToken(GlobalContext.getCacheToken());
			Command command = new Command();
			command.addArgs("mobile", mobile).addArgs("npwd", Utils.Md5(password))
			.addArgs("code", code).addAction(Action.ACTION_FORGET_PASSWORD);
			batch.addCommand(command);
			stringEntity = new StringEntity(Batch.build().addCommand(command).toJson(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		HttpUtil http = new HttpUtil();
		http.req(mContext, stringEntity, new MyResponse() {
			
			@Override
			public void responseSucess(Object obj) {
				// TODO Auto-generated method stub
				if (null != mListener)
					mListener.onSuccess();
			}
			
			@Override
			public void responseFailure(int errorCode, String msg) {
				// TODO Auto-generated method stub
				if (null != mListener)
					mListener.onFailure(errorCode, msg);
			}
		});
	}

	@Override
	public boolean addListener(IForgetPassListener listener) {
		mListener = listener;
		return mListener == null ? false : true;
	}

	@Override
	public boolean removeListener(IForgetPassListener listener) {
		if (null == listener)
			return false;
		mListener = null;
		return true;
	}
	
	@Override
	public boolean addAuthListener(IAuthCodeListener listener) {
		authListener = listener;
		return authListener == null ? false : true;
	}

	@Override
	public boolean removeAuthListener(IAuthCodeListener listener) {
		if (null == listener)
			return false;
		authListener = null;
		return true;
	}
	
	
}
